<?php
/**
 * @Name: UserNodeLogic.php
 * @Author: yashuai<1762910894@qq.com>
 */

namespace App\Http\Logic\Rbac;

use App\Http\Logic\BaseLogic;
use App\Http\Model\Rbac\RoleModel;
use App\Http\Model\Rbac\UserRoleModel;

class UserRoleLogic extends BaseLogic
{

    /**
     * @Desc 用户设置角色
     * @return bool
     * @throws \App\Exceptions\ThrowException
     * @author yashuai<1762910894@qq.com>
     */
    public function setUserRole()
    {
        $role = RoleModel::query()->whereIn('RoleID', $this->params['RoleID'])->pluck('RoleID')->toArray();
        if (!empty($role)) {
            $data = [];
            foreach ($role as $roleID) {
                $data[] = [
                    'RoleID' => $roleID,
                    'UserID' => $this->params['UserID'],
                ];
            }
            UserRoleModel::query()->where(['UserID' => $this->params['UserID']])->delete();
            UserRoleModel::newInsert($data);
        }
        return true;
    }

}
